package com.ahp.service.impl;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ahp.bean.EvaleObject;
import com.ahp.bean.Tast;
import com.ahp.dao.EvaleObjectDao;
import com.ahp.dao.TaskDao;
import com.ahp.service.EvaleObjectService;

public class EvaleObjectServiceImpl implements EvaleObjectService {
	private EvaleObjectDao evaleObjectDao;
	private TaskDao taskDao;
	
	public EvaleObjectDao getEvaleObjectDao() {
		return evaleObjectDao;
	}

	public void setEvaleObjectDao(EvaleObjectDao evaleObjectDao) {
		this.evaleObjectDao = evaleObjectDao;
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#addEvaleObject(com.ahp.bean.EvaleObject)
	 */
	public Integer addEvaleObject(EvaleObject evaleObject) {
		evaleObject.setTast((Tast)taskDao.load(evaleObject.getTast().getTastId()));
		Integer num = (Integer)evaleObjectDao.save(evaleObject);
		return num;
	}

	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#findEvaleObject(java.lang.Integer)
	 */
	public List<EvaleObject> findEvaleObject(Integer id) {
		String hql = "from EvaleObject eo where eo.tast.tastId=?";
		return evaleObjectDao.queryInfo(hql, id);
	}
	
	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#delEvaleObject(java.lang.Integer)
	 */
	public boolean delEvaleObject(EvaleObject evaleObject) {
		try {
			evaleObjectDao.delete(evaleObject);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#findEvaleObjects(java.util.List)
	 */
	public List<EvaleObject> findEvaleObjects(List<Integer> list, Integer taskId) {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		for (Integer i : list) {
			sb.append(i).append(",");
		}
		String str = sb.substring(0, sb.length()-1)+")";
		String hql = "from EvaleObject eo where eo.tast.tastId=? and eo.id in "+str;
		
		return evaleObjectDao.queryInfo(hql, taskId);
	}
	
	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#objectIdToName(java.util.Map)
	 */
	public Map<String, Float> objectIdToName(Map<String, Float> map) {
		Map<String, Float> map1 = new LinkedHashMap<String, Float>();
		String hql = "from EvaleObject eo where eo.evaleId=";
		for(Iterator<String> it = map.keySet().iterator();it.hasNext();){
			String id = it.next();
			List<EvaleObject> list = evaleObjectDao.find(hql+id);
			String name = list.get(0).getName();
			map1.put(name, map.get(id));
		}
		return map1;
	}
	
	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#saveObjectScore(java.util.Map)
	 */
	public void saveObjectScore(Map<String, Float> map) {
		
		for(Iterator<String> it = map.keySet().iterator(); it.hasNext();){
			String key = it.next();
			EvaleObject eo = (EvaleObject)evaleObjectDao.get(EvaleObject.class, Integer.parseInt(key));
			eo.setScore(map.get(key));
			evaleObjectDao.update(eo);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.ahp.service.EvaleObjectService#listObject(java.lang.Integer)
	 */
	public List<EvaleObject> listObject(Integer taskId) {
		String hql = "from EvaleObject eo where eo.tast.tastId="+taskId;
		return evaleObjectDao.find(hql);
	}
	
	public Tast getTask(Integer id) {
		return (Tast)taskDao.get(Tast.class, id);
	}
}
